package com.leo.stockmarket.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leo.stockmarket.bean.Article;
import com.leo.stockmarket.dao.ArticleDao;
import com.leo.stockmarket.util.PageRequest;
import com.leo.stockmarket.util.PageResult;
import com.leo.stockmarket.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 890231 lxj
 * @version 1.0
 * @date 2020/8/19 10:02
 */
@Service
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    ArticleDao articleDao;

    @Override
    public Integer insert(Article article) {
        return articleDao.insert(article);
    }

    @Override
    public PageResult queryPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    @Override
    public Article queryById(String businessId) {
        return articleDao.queryArticleById(businessId);
    }

    @Override
    public List<Article> queryListBySubjectId(String subjectId, String type) {
        return articleDao.queryListBySubjectId(subjectId, type);
    }

    /**
     * 调用分页插件完成分页
     * @param pageRequest
     * @return
     */
    private PageInfo<Article> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleDao.queryList();
        return new PageInfo<Article>(articles);
    }
}
